//时间工具类
/**
 * 获取本周、本季度、本月、上月的开始日期、结束日期
 */
let now = new Date(); //当前日期
let nowDayOfWeek = now.getDay(); //今天本周的第几天
let nowDay = now.getDate(); //当前日
let nowMonth = now.getMonth(); //当前月
let nowYear = now.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //

let lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
let lastYear = lastMonthDate.getYear();
let lastMonth = lastMonthDate.getMonth();


//格式化日期：yyyy-MM-dd
function formatDate(date = new Date(), type = 3) {
	//type=0不用0补位；
	//type==1返回月份不带天
	//type==2返回月份不带天不用0补位
	//type==3返回年月日时分秒

	let dateTime = new Date(date)
	let myyear = dateTime.getFullYear();
	let mymonth = dateTime.getMonth() + 1;
	let myDay = dateTime.getDate();
	let myTime = dateTime.toTimeString().slice(0, 8)

	switch (type) {
		case 0:
			return (myyear + "-" + mymonth + "-" + myDay);
		case 1:
			if (mymonth < 10) {
				mymonth = "0" + mymonth;
			}
			if (myDay < 10) {
				myDay = "0" + myDay;
			}
			return (myyear + "-" + mymonth);
		case 2:
			return (myyear + "-" + mymonth);
		case 3:
			if (mymonth < 10) {
				mymonth = "0" + mymonth;
			}
			if (myDay < 10) {
				myDay = "0" + myDay;
			}
			return (myyear + "-" + mymonth + "-" + myDay + " " + myTime);
		default:
			if (mymonth < 10) {
				mymonth = "0" + mymonth;
			}
			if (myDay < 10) {
				myDay = "0" + myDay;
			}
			return (myyear + "-" + mymonth + "-" + myDay);

	}
}
//获得某月的天数
function getMonthDays(myMonth) {
	let monthStartDate = new Date(nowYear, myMonth, 1);
	let monthEndDate = new Date(nowYear, myMonth + 1, 1);
	let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
	return days;
}
//获得本季度的开始月份
function getQuarterStartMonth() {
	let quarterStartMonth = 0;
	if (nowMonth < 3) {
		quarterStartMonth = 0;
	}
	if (2 < nowMonth && nowMonth < 6) {
		quarterStartMonth = 3;
	}
	if (5 < nowMonth && nowMonth < 9) {
		quarterStartMonth = 6;
	}
	if (nowMonth > 8) {
		quarterStartMonth = 9;
	}
	return quarterStartMonth;
}
//获得本周的开始日期
function getWeekStartDate(type) {
	let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
	return formatDate(weekStartDate, type);

}
//获得本周的结束日期
function getWeekEndDate(type) {
	let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
	return formatDate(weekEndDate, type);

}
//获得上周的开始日期
function getLastWeekStartDate(type) {
	let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
	return formatDate(weekStartDate, type);

}
//获得上周的结束日期
function getLastWeekEndDate(type) {
	let weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);
	return formatDate(weekEndDate, type);

}
//获得上上或下下月开始时间
function getMonthStartDate(type, nums) {
	let nextMonthDate = new Date(); //查询最近月份日期
	nextMonthDate.setDate(1);
	if (nums == 0) {
		//本月
	} else if (nums) {
		nextMonthDate.setMonth(nextMonthDate.getMonth() + nums);
	}
	let nextYear = nextMonthDate.getYear();
	let nextMonth = nextMonthDate.getMonth();
	let nextMonthStartDate = new Date(nowYear, nextMonth, 1);
	return formatDate(nextMonthStartDate, type);

}
//获得上上或下下月结束时间
function getMonthEndDate(type, nums) {
	let nextMonthDate = new Date(); //查询最近月份日期
	nextMonthDate.setDate(1);
	if (nums == 0) {
		//本月
	} else if (nums) {
		nextMonthDate.setMonth(nextMonthDate.getMonth() + nums);
	}
	let nextYear = nextMonthDate.getYear();
	let nextMonth = nextMonthDate.getMonth();
	let nextMonthEndDate = new Date(nowYear, nextMonth, getMonthDays(nextMonth));
	return formatDate(nextMonthEndDate, type);

}
//获得本季度的开始日期
function getQuarterStartDate(type) {
	let quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
	return formatDate(quarterStartDate, type);

}
//或的本季度的结束日期
function getQuarterEndDate(type) {
	let quarterEndMonth = getQuarterStartMonth() + 2;
	let quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
	return formatDate(quarterStartDate, type);

}
//获取两日期之间日期列表函数
function getdiffdate(stime, etime) {
	//初始化日期列表，数组
	let diffdate = new Array();
	let i = 0;
	//开始日期小于等于结束日期,并循环
	while (stime <= etime) {
		diffdate[i] = stime;

		//获取开始日期时间戳
		let stime_ts = new Date(stime.replace(/-/g, "/")).getTime();
		// console.log('当前日期：'+stime   +'当前时间戳：'+stime_ts);

		//增加一天时间戳后的日期
		let next_date = stime_ts + (24 * 60 * 60 * 1000);

		//拼接年月日，这里的月份会返回（0-11），所以要+1
		let next_dates_y = new Date(next_date).getFullYear() + '-';
		let next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) +
			'-' : (new Date(next_date).getMonth() + 1) + '-';
		let next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(
			next_date).getDate();

		stime = next_dates_y + next_dates_m + next_dates_d;

		//增加数组key
		i++;
	}
	return diffdate
}

function getCurrentDate(time) {
	let timeData = new Date();
	if (time) {
		timeData = new Date(time.replace(/-/g, "/"))
	}
	let str = timeData.getFullYear().toString() + '-' + addZero(timeData.getMonth() + 1) + '-' + addZero(timeData
		.getDate()) + ' ' + addZero(timeData.getHours()) + ':' + addZero(timeData.getMinutes()) + ':' + addZero(
		timeData.getSeconds());
	return str
}
//转化时间戳 s-10位 默认13
function time13(stime, type) {
	let time = new Date().getTime();
	if (stime) {
		time = new Date(stime.replace(/-/g, "/")).getTime();
	}
	if (type = 's') {
		return time / 1000
		console.log(time, '这是time')
	} else {
		return time
	}
}
//秒杀状态判断 返回为秒
function seckillType(startTime, endTime) {
	let timeType = "距离开始";
	let time = 0;
	if (time13(startTime, "s") - time13(false, "s") > 0) {
		timeType = "距离开始"

		time = (time13(startTime, "s") - time13(false, "s")).toFixed(0)
		return {
			timeType: timeType,
			time: time
		}

	} else if (time13(endTime, 's') - time13(false, "s") > 0) {

		time = (time13(endTime, 's') - time13(false, "s")).toFixed(0)

		timeType = "距离结束"
		return {
			timeType: timeType,
			time: time
		}
	} else {
		timeType = "已结束"
		return {
			timeType: timeType,
			time: time
		}
	}
}

function addZero(v) {
	if (v < 10) return '0' + v;
	return v.toString();
}
export default {
	seckillType,
	time13,
	getCurrentDate,
	formatDate,
	getWeekStartDate,
	getWeekEndDate,
	getdiffdate,
	getMonthEndDate,
	getMonthStartDate
}
